/**
 * @author sunyu
 * 初始化设置时间公共js
 *
 */

//
var date = new Date();
var nowMs = date.getTime();
var sevenDays = nowMs - 5184e5;//前第六天
var startTime0 = $.formatDate("yyyy-MM-dd",sevenDays);
var endTime0 =  $.formatDate("yyyy-MM-dd",nowMs);
//设置默认时间
var $startTime = $("#startTime");
var $endTime = $("#endTime");
$startTime.val(startTime0);
$endTime.val(endTime0);
function setDefaultTime(){

}
//时间选项添加事件监听
$("#quickSelect").on('change',function(){
    var value = $(this).val();
    if(value==1){
        //昨天
        var yesterday = nowMs-864e5;
        var startTime = $.formatDate("yyyy-MM-dd",yesterday);
        $startTime.val(startTime);
        $endTime.val(startTime);
    }else if(value==2){
        //近七天
        var sevenDays = nowMs - 5184e5;//前第六天
        var startTime = $.formatDate("yyyy-MM-dd",sevenDays);
        var endTime =  $.formatDate("yyyy-MM-dd",nowMs);
        $startTime.val(startTime);
        $endTime.val(endTime);
    }else  if(value ==3){
        var startTime = $.boco.getFirstDayOfCurrentWeek(nowMs);
        startTime = $.formatDate("yyyy-MM-dd",startTime);
        var endTime =  $.formatDate("yyyy-MM-dd",nowMs);
        $startTime.val(startTime);
        $endTime.val(endTime);
        //本周
    }else if(value ==4){
        //上周
        var startTime = $.boco.getFistDayOfLastWeek(nowMs);
        var endTime = startTime + 5184e5;
        startTime = $.formatDate("yyyy-MM-dd",startTime);
        endTime =  $.formatDate("yyyy-MM-dd",endTime);
        $startTime.val(startTime);
        $endTime.val(endTime);
    }else if(value == 5){
        //本月
        var startTime = $.boco.getFirstDayOfCurrentMonth(nowMs);
        startTime = $.formatDate("yyyy-MM-dd",startTime);
        var endTime =  $.formatDate("yyyy-MM-dd",nowMs);
        $startTime.val(startTime);
        $endTime.val(endTime);
    }else if(value == 6){
        //上月
        var startTime = $.boco.getFistDayOfLastMonth(nowMs);
        startTime = $.formatDate("yyyy-MM-dd",startTime);
        var endTime = $.boco.getFirstDayOfCurrentMonth(nowMs);
        endTime = $.formatDate("yyyy-MM-dd",(endTime-864e5));

        $startTime.val(startTime);
        $endTime.val(endTime);
    }
})
//获取机构类别
function setInstitutionList(selectId,value){
	$.ajax({  
		type: "GET",
		url: "../pageCommon/queryInstitutionTypeList",
		success:function(data){ 
			if(data != ""){
				//生成option
				$option = $("<option value=''>全部</option>");
				$(selectId).append($option);
				if(data.length > 0 ){
					for (var i = 0; i < data.length; i++) {
						$option = $("<option></option>");
						$option.attr("value",data[i].jgbm);
						$option.text(data[i].jgmc);
						if(data[i].jgbm==value){
							$option.attr("selected",true);
						}
						$(selectId).append($option);
					}
				}
			}  
		},  
		error:function(data){  
			alert("出现异常，异常原因【" + data.length + "】!");    
		}
	});
}
//获取管辖区域列表列表 
function setManagementAreaList(selectId,value){
	$.ajax({  
		type: "GET",
		url: "../pageCommon/queryManagementArea",  
		success:function(data){ 
			if(data != ""){
				//生成option
				$option = $("<option value=''>全部</option>");
				$(selectId).append($option);
				if(data.length > 0 ){
					for (var i = 0; i < data.length; i++) {
						$option = $("<option></option>");
						$option.attr("value",data[i].gxqybm);
						if(data[i].gxqybm==value){
							$option.attr("selected",true);
						}
						$option.text(data[i].qymc);
						$(selectId).append($option);
					}
				}
			}  
		},  
		error:function(data){  
			alert("出现异常，异常原因【" + data.length + "】!");    
		}
	});
}
/**
 * 获取机构列表数据绘制管辖区域选项框
 * @param selectId  
 * @param value 需要选中的管辖区域
 */
function setManagementAreacheckBox(selectId,value){
	$.ajax({  
		type: "GET",
		url: "../pageCommon/queryManagementArea", 
		async:false,
		success:function(data){ 
			$(selectId).empty();//清空原有数据
			if(data){
				//生成option
				$option = $("<option value=''>全部</option>");
				$(selectId).append($option);
				if(data.length > 0 ){
					
					var checkboxHtml = '<input type="checkbox" id="managementArea-checkAll" value="" name="managementArea" onclick="checkAllManagementArea(this)" checked="checked"/>全部';
					if(data.length > 0 ){
						for (var i = 0,len = data.length; i < len; i++) {
							var obj = data[i];
							var checked=false;
							if(obj.ksmc==value){checked=true}
							checkboxHtml += '<input type="checkbox" name="managementArea" value="'+obj.gxqybm+'" checked="'+checked+'" onclick="removeManagementArea();">'+obj.gxqy;
						}
						$(selectId).html(checkboxHtml);
					}
				}
			}		
		},  
		error:function(data){  
			alert("出现异常，异常原因【" + data.length + "】!");    
		}
	});
	
}
/**
 * 管辖区域全选
 * @param obj
 */
function checkAllManagementArea(obj){
	$("#managementArea-checkbox input[type='checkbox']").prop('checked', $(obj).prop('checked'));
	
}
/**
 * 移除管辖区域全选
 */
function removeManagementArea(){
	$("#managementArea-checkAll").prop('checked', false);
}
/**
 *  获取科室列表(根据机构查询)
 * @param selectId
 */
function setDepartmentList(selectId,jgList){
	$.ajax({
		type: "GET",
		url: "../pageCommon/queryDepartment",
		data:{
			institutions:jgList
		},
		async:false,
		success:function(data){
			$(selectId).empty();//清空原有数据
			if(data){
				//绘制checkbox
				var checkboxHtml = '<input type="checkbox" id="department-checkAll" value="" name="department" onclick="checkAllDepartments(this)" checked="checked"/>全部';
				if(data.length > 0 ){
					for (var i = 0,len = data.length; i < len; i++) {
						var obj = data[i];
						checkboxHtml += '<input type="checkbox" name="department" value="'+obj.kskey+'" checked="checked" onclick="removeDepartmentCheck();">'+obj.ksmc;
					}
					$(selectId).html(checkboxHtml);
				}
			}
		},
		error:function(data){

		}
	});
}
/**
 * 科室全选不全选
 * @param obj
 */
function checkAllDepartments(obj){
	$("#department-checkbox input[type='checkbox']").prop('checked', $(obj).prop('checked'));

}
/**
 * 去除科室全选勾选项
 */
function removeDepartmentCheck(){
	$("#department-checkAll").prop('checked', false);
}
/**
 * 获取机构列表数据绘制机构选项框,根据机构类别获取
 */
function setInstitutionCheckBox(selectId,params){
	$.ajax({
		type: "GET",
		url: "../pageCommon/queryInstitutionList",
		data:params,
		async:false,
		success:function(data){
			if(data){
				//绘制checkbox,写null被丢弃，否则会出现on
				var checkboxHtml = '<input type="checkbox" id="institution-checkAll" value="" name="institution" onclick="checkAllInstitutions(this)" checked="checked"/>全部';
				if(data.length > 0 ){
					for (var i = 0,len = data.length; i < len; i++) {
						var obj = data[i];
						checkboxHtml += '<input type="checkbox" name="institution" value="'+obj.jgbm+'" checked="checked" onclick="removeInstitutionsBoxStatus();">'+obj.jgmc;
					}
					$(selectId).html(checkboxHtml);
				}
			}
		},
		error:function(data){

		}
	});
}

/**
 * 选择所有的机构
 */
function checkAllInstitutions(obj){
	$("#institution-checkbox input[type='checkbox']").prop('checked', $(obj).prop('checked'));
	//重新刷新科室
	var institutionValue = $.boco.getCheckedValues('institution', '#institution-checkbox');//获取机构列表
	$(setDepartmentList($("#department-checkbox"),institutionValue.join(",")));
}
/**
 * 点击其中一个机构移除全选框的选中中
 */
function removeInstitutionsBoxStatus(){
	$("#institution-checkAll").prop('checked',false);
	//重新刷新科室
	var institutionValue = $.boco.getCheckedValues('institution', '#institution-checkbox');//获取机构列表
	$(setDepartmentList($("#department-checkbox"),institutionValue.join(",")));
}
/**
 * 选择指标
 * @param obj
 */
function checkIndicator(obj){
	$("#indicator-checkbox input[type='checkbox']").prop('checked', $(obj).prop('checked'));
}
/**
 * 点击其中一个指标，将全选的选中去掉
 */
function removeIndicatorAll(){
	$("#indicator-checkAll").prop("checked",false);
}
/**
 * 获取管辖区域列表数据绘制机构选项框,根据管辖区域类别获取
 */
function setRegionCheckBox(selectId,params){
	$.ajax({
		type: "GET",
		url: "../pageCommon/queryManagementArea",
		data:params,
		async:false,
		success:function(data){
			if(data){
				//绘制checkbox,写null被丢弃，否则会出现on
				var checkboxHtml = '<input type="checkbox" id="region-checkAll" value="" name="region" onclick="checkAllRegion(this)" checked="checked"/>全部';
				if(data.length > 0 ){
					for (var i = 0,len = data.length; i < len; i++) {
						var obj = data[i];
						checkboxHtml += '<input type="checkbox" name="region" value="'+obj.gxqybm+'" checked="checked" onclick="removeRegionBoxStatus();">'+obj.qymc;
					}
					$(selectId).html(checkboxHtml);
				}
			}
		},
		error:function(data){
			alert(0);
		}
	});
}
/**
 * 选择所有的管辖区域
 */
function checkAllRegion(obj){
	$("#region-checkbox input[type='checkbox']").prop('checked', $(obj).prop('checked'));
	//重新刷新机构
	var managementValue = $.boco.getCheckedValues('region', '#region-checkbox');//获取管辖区域
	$(setInstitutionByManagement($("#institution-checkbox"),managementValue.join(",")));
	//重新刷新科室
	var institutionValue = $.boco.getCheckedValues('institution', '#institution-checkbox');//获取机构列表
	$(setDepartmentList($("#department-checkbox"),institutionValue.join(",")));
}
/**
 * 点击其中一个管辖区域移除全选框的选中中
 */
function removeRegionBoxStatus(){
	$("#region-checkAll").prop('checked',false);
	//重新刷新机构
	var managementValue = $.boco.getCheckedValues('region', '#region-checkbox');//获取管辖区域
	$(setInstitutionByManagement($("#institution-checkbox"),managementValue.join(",")));
	//重新刷新科室
	var institutionValue = $.boco.getCheckedValues('institution', '#institution-checkbox');//获取机构列表
	$(setDepartmentList($("#department-checkbox"),institutionValue.join(",")));
}
//根据管辖区域代码获取机构
function setInstitutionByManagement(selectId,regionList){
	$.ajax({
		type: "GET",
		url: "../pageCommon/queryInstitutionByManagement",
		data:{
			regions:regionList
		},
		async:false,
		success:function(data){
			if(data){
				//绘制checkbox,写null被丢弃，否则会出现on
				var checkboxHtml = '<input type="checkbox" id="institution-checkAll" value="" name="institution" onclick="checkAllInstitutions(this)" checked="checked"/>全部';
				if(data.length > 0 ){
					for (var i = 0,len = data.length; i < len; i++) {
						var obj = data[i];
						checkboxHtml += '<input type="checkbox" name="institution" value="'+obj.jgbm+'" checked="checked" onclick="removeInstitutionsBoxStatus();">'+obj.jgmc;
					}
					$(selectId).html(checkboxHtml);
				}
			}
		},
		error:function(data){
			alert(0);
		}
	});
}